package com.drinks.yiniang.common.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.drinks.yiniang.common.bitmap.PBitmapUtils;
import com.drinks.yiniang.entity.photo.Photos;
import com.drinks.yiniang.ui.widget.image.CropFixImageView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.ScreenSizeUtils;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author han
 * @date 2019/12/25
 */
public class CropViewContainerHelper {
    private WeakReference<ViewGroup> parentReference;
    //存储已选择的剪裁View
    private HashMap<Photos, CropFixImageView> cropViewList = new HashMap<>();

    private CropFixImageView mCropView;

    public CropViewContainerHelper(@NonNull ViewGroup parent) {
        parentReference = new WeakReference<>(parent);
    }

    private ViewGroup getParent() {
        if (parentReference != null && parentReference.get() != null) {
            return parentReference.get();
        }
        return null;
    }

    public void setBackgroundColor(int color) {
        if (mCropView != null) {
            mCropView.setBackgroundColor(color);
        }
    }

    public CropFixImageView loadCropView(final Context context, final Photos imageItem) {
        if (cropViewList.containsKey(imageItem) && cropViewList.get(imageItem) != null) {
            mCropView = cropViewList.get(imageItem);
        } else {
            mCropView = new CropFixImageView(context);
            mCropView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //设置剪裁view的属性

            mCropView.setOnImageLoadListener(new CropFixImageView.OnImageLoadListener() {
                @Override
                public void onImageLoaded(float w, float h) {
                    imageItem.width = (int) w;
                    imageItem.height = (int) h;
                }
            });

            GlideUtils.loadARGB8888(context, mCropView, imageItem.path);
        }

        if (getParent() != null) {
            //循环里添加cropimageview，过快导致前面几个的drawable为空, 后续想办法解决
            //getParent().removeAllViews();
            if (mCropView.getParent() != null) {
                ((ViewGroup) mCropView.getParent()).removeView(mCropView);
            }
            getParent().addView(mCropView);
        }
        return mCropView;
    }

    public void addCropView(CropFixImageView view, Photos imageItem) {
        if (!cropViewList.containsKey(imageItem)) {
            cropViewList.put(imageItem, view);
        }
    }

    public void removeCropView(Photos imageItem) {
        cropViewList.remove(imageItem);
    }

    public ArrayList<Photos> generateCropUrls(List<Photos> selectList) {
        for (Photos imageItem : selectList) {
            CropFixImageView view = cropViewList.get(imageItem);
            if (view == null) {
                continue;
            }
            view.requestLayout();
            Bitmap bitmap;
            bitmap = view.generateCropBitmap();
            String cropUrl = PBitmapUtils.saveBitmapToFile(view.getContext(), bitmap,
                    "crop_" + System.currentTimeMillis(),
                    Bitmap.CompressFormat.JPEG);
            if (imageItem.cropPath != null && imageItem.cropPath.length() > 0) {
                new File(imageItem.cropPath).delete();
            }
            imageItem.cropPath = cropUrl;
        }
        return (ArrayList<Photos>) selectList;
    }

    public Photos generateCropUrl(Photos imageItem) {
        CropFixImageView view = cropViewList.get(imageItem);

        if (view == null) {
            return imageItem;
        }

        view.requestLayout();
        Bitmap bitmap;
        bitmap = view.generateCropBitmap();
        String cropUrl = PBitmapUtils.saveBitmapToFile(view.getContext(), bitmap,
                "crop_" + System.currentTimeMillis(),
                Bitmap.CompressFormat.JPEG);
        if (imageItem.cropPath != null && imageItem.cropPath.length() > 0) {
            new File(imageItem.cropPath).delete();
        }

        imageItem.cropPath = cropUrl;

        return imageItem;
    }
}
